<template>
	<view class="main1">
<!-- 		<view class="box">
			<view class="part">
				<view class="text">编号：</view>
				<view class="input">
					<input type="text" v-model="model.c_id">
				</view>				
			</view>
			<view class="part">
				<view class="text">姓名：</view>
				<view class="input">
					<input type="text" v-model="model.c_name">
				</view>				
			</view>
			<view class="part">
				<view class="text">性别：</view>
				<view class="input">
<radio-group @change="radioChange">
	<label class="radio-group" >
		<radio class="radio" value="1" :checked="model.c_sex === 1" ><text>男</text></radio>
		<radio class="radio" value="0" :checked="model.c_sex === 0"><text>女</text></radio>
	</label>
</radio-group>
				</view>				
			</view>
			<view class="part">
				<view class="text">电话：</view>
				<view class="input">
					<input type="text" v-model="model.c_phone">
				</view>				
			</view>
			<view class="part">
				<view class="text">地址：</view>
				<view class="input">
					<input type="text" v-model="model.c_address">
				</view>				
			</view>
			<view class="part">
				<view class="text">年龄：</view>
				<view class="input">
					<input type="text" v-model="model.c_age">
				</view>				
			</view>
			<view class="part1">
			
			</view>
			<button type="primary" @tap="saveClick(model.AutoID)">保存</button>
		</view> -->
		
<!-- 		客户信息：{{model.c_id}}-{{model.c_name}}-{{msg}}
		<uni-list>
			<uni-list-item  :title="'编号：' + model.c_id"></uni-list-item>
			<uni-list-item  :title="model.c_name"></uni-list-item>
			<uni-list-item  :title="model.c_sex?'男':'女'"></uni-list-item>
			<uni-list-item  :title="model.c_phone"></uni-list-item>
			<uni-list-item  :title="model.c_address"></uni-list-item>
			<uni-list-item  :title="model.c_age"></uni-list-item>
		</uni-list> -->
		<detail-main :model="model" @edit-click = "editClick"></detail-main>
	</view>
</template>

<script>
	import detailMain from "../../components/detail-main/detail-main.vue";
	export default {
		comments:{
			detailMain
		},
		data() {
			return {
				model: {c_id:'',
				c_name:'',
				c_sex:'',
				c_phone:'',
				c_address:'',
				c_age:''},
				msg: ''
			}
		},
		onLoad(options) {
			// console.log(options);
			// if (options.c_id) {
			// 	this.customerid = options.c_id
			// }
			const eventChannel = this.getOpenerEventChannel()
			// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
			eventChannel.on('send',(data)=> {
				console.log(data);
				this.model.AutoID = data.AutoID
				// this.model = data
				if (this.model.AutoID) {
					this.fetch(this.model.AutoID)
				}
					});
			// uni.$on() 在里面要使用临时变量var _this = this;
			let _this = this
			  uni.$on('update',(data)=>{
			        console.log('监听到事件来自 update ，携带参数 msg 为：' + data.msg);
					
					this.msg =  data.msg;
					
					
			    })
			
		},
		onUnload() {
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.off('send');
			uni.$off('send')
		},
		methods: {
			radioChange(evt) {
				console.log(evt.detail.value)
				this.model.c_sex = evt.detail.value
			},
			saveClick(id) {
				console.log(id)
					uni.request({
					url: `${this.apiUrl}/rest/ad/${id}`,
					method: 'PUT',
					data: {id: this.model.AutoID,...this.model},
					success: (res) => {
						console.log(res)
						if (res.data.code === 200) {
							uni.showToast({
								title:res.data.message,
								icon:'success'
							})
						}
					},
					fail: () => {
				
					},
					complete: () => {
				
					}
				})
			},
			editClick(model) {
			console.log(model,this.model,model === this.model)
			const id = this.model.AutoID
			this.model = model
				uni.request({
					url: `${this.apiUrl}/rest/ad/${id}`,
					method: 'PUT',
					data: {
						id: this.model.AutoID,
						...this.model
					},
					success: (res) => {
						console.log(res)
						if (res.data.code === 200) {
							uni.showToast({
								title: res.data.message,
								icon: 'success'
							})
						}
					},
					fail: () => {
			
					},
					complete: () => {
			
					}
				})	
			},
			fetch(id) {
			  uni.request({
			  	url: `${this.apiUrl}/rest/ad/${id}`,
			  	method: 'GET',
			  	data: {},
			  	success: (res) => {
					 this.model = Object.assign({}, this.model, res.data);
			  		console.log(this.items)
			  	},
			  	fail: () => {
			  
			  	},
			  	complete: () => {
			  
			  	}
			  })
			},
		}
	}
</script>

<style>
	.radio-group {
	/* 	text-align: left; */
	}
	.radio {
		width: 200rpx;
		
	}
	.text {
		font-size:large;
		width: 20%;
		display: flex;
		align-items:center;
/* 		flex-direction: column;
		justify-content: center; */
	}
	.input {
		font-size:large;
		border: 1rpx solid #EEEEEE;
			/* 	height: 50rpx; */
				margin: 0 10rpx;
			/* 	line-height: 50rpx; */
				color: red;
				width: 80%;
		display: flex;

		justify-content: center;
		align-items: center;
	}
	.part {
		display: flex;
		justify-content: center;
		width:100%;
		height: 40px;
		border-bottom: #C0C0C0 1px solid;
	}
	.part1 {
		display: flex;
		justify-content: center;
		width:100%;
		height: 40px;
		border-bottom: #C0C0C0 1px solid;
	}
	.box {
		width: 90%;
/* 		height: 200px;
		background-color:#007AFF; */
	}
.main {
	display: flex;
	justify-content: center;
	width: 100%;
}

</style>
